package cn.autumnorange.service.impl;

import cn.autumnorange.dto.*;
import cn.autumnorange.entity.OrderEntity;
import cn.autumnorange.enums.DeliveryStatusEnum;
import cn.autumnorange.enums.OrderStatusEnum;
import cn.autumnorange.enums.ResultEnum;
import cn.autumnorange.exception.ChanelException;
import cn.autumnorange.repository.OrderRepository;
import cn.autumnorange.service.OrderService;
import cn.autumnorange.util.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName OrderServiceImpl
 * @Description
 * @Date 2019/4/11 14:02
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Override
    @Transactional
    public CreateOrderDTO createOrder(CreateOrderDTO createOrderDTO) {
        // 生成订单id
        String order = KeyUtil.genUniqueKey();

        //写入订单数据库
        OrderEntity orderEntity = new OrderEntity();
        BeanUtils.copyProperties(createOrderDTO, orderEntity);
        orderEntity.setOrderId(order);
        orderEntity.setOrderStatus(OrderStatusEnum.WAIT.getCode());
        orderEntity.setDeliveryStatus(DeliveryStatusEnum.UNSHIPPED.getCode());
        orderEntity = orderRepository.save(orderEntity);

        //返回订单详情
        BeanUtils.copyProperties(orderEntity, createOrderDTO);
        return createOrderDTO;
    }

    @Override
    public EditOrderDTO editOrder(EditOrderDTO editOrderDTO) {
        //判断订单状态和发货状态是否产生逻辑冲突
        if (OrderStatusEnum.WAIT.getCode().equals(editOrderDTO.getOrderStatus()) && DeliveryStatusEnum.SHIPPED.getCode().equals(editOrderDTO.getDeliveryStatus())){
            throw new ChanelException(ResultEnum.ORDER_STATUS_ERROR);
        }
        //先查订单是否存在
        if (orderRepository.existsById(editOrderDTO.getOrderId())){
            //写入订单数据库
            OrderEntity orderEntity = orderRepository.getOne(editOrderDTO.getOrderId());
            orderEntity.setDeliveryStatus(editOrderDTO.getDeliveryStatus());
            orderEntity.setOrderStatus(editOrderDTO.getOrderStatus());
            orderEntity.setBuyerAddress(editOrderDTO.getBuyerAddress());
            orderEntity.setBuyerName(editOrderDTO.getBuyerName());
            orderEntity.setBuyerPhone(editOrderDTO.getBuyerPhone());
            orderEntity.setCourierNumber(editOrderDTO.getCourierNumber());
            orderEntity.setPostageStatus(editOrderDTO.getPostageStatus());
            orderEntity.setProductName(editOrderDTO.getProductName());
            orderEntity = orderRepository.save(orderEntity);

            //返回订单信息
            BeanUtils.copyProperties(orderEntity, editOrderDTO);
            return editOrderDTO;
        }
        return null;
    }

    @Override
    public boolean deleteOrder(String orderId) {
        //判断订单是否存在
        if (orderRepository.existsById(orderId)){
            //删除订单
            orderRepository.deleteById(orderId);
            return true;
        }
        return false;
    }

    @Override
    public AuditOrderDTO auditOrder(String orderId, int orderStatus) {
        //判断订单是否存在
        if (!orderRepository.existsById(orderId)){
            throw new ChanelException(ResultEnum.ORDER_NOT_EXIST);
        }
        //修改订单状态
        OrderEntity orderEntity = orderRepository.getOne(orderId);
        orderEntity.setOrderStatus(orderStatus);
        orderEntity = orderRepository.save(orderEntity);

        //返回订单信息
        AuditOrderDTO auditOrderDTO = new AuditOrderDTO();
        BeanUtils.copyProperties(orderEntity, auditOrderDTO);
        return auditOrderDTO;
    }

    @Override
    public DeliverGoodsDTO deliverGoods(String orderId, int deliveryStatus) {
        //判断订单是否存在
        if (!orderRepository.existsById(orderId)){
            throw new ChanelException(ResultEnum.ORDER_NOT_EXIST);
        }

        //判断订单的审核状态
        OrderEntity orderEntity = orderRepository.getOne(orderId);
        if (!OrderStatusEnum.FINISHED.getCode().equals(orderEntity.getOrderStatus())){
            throw new ChanelException(ResultEnum.ORDER_STATUS_ERROR);
        }

        //修改订单状态
        orderEntity.setDeliveryStatus(deliveryStatus);
        orderEntity = orderRepository.save(orderEntity);

        //返回订单信息
        DeliverGoodsDTO deliverGoodsDTO = new DeliverGoodsDTO();
        BeanUtils.copyProperties(orderEntity, deliverGoodsDTO);
        return deliverGoodsDTO;
    }

    @Override
    public CourierNumberDTO courierNumber(String orderId, String courierNumber) {
        //判断订单是否存在
        if (!orderRepository.existsById(orderId)){
            throw new ChanelException(ResultEnum.ORDER_NOT_EXIST);
        }
        //填写快递单号，更新到订单数据库
        OrderEntity orderEntity = orderRepository.getOne(orderId);
        orderEntity.setCourierNumber(courierNumber);
        orderEntity = orderRepository.save(orderEntity);

        //返回订单信息
        CourierNumberDTO courierNumberDTO = new CourierNumberDTO();
        BeanUtils.copyProperties(orderEntity, courierNumberDTO);
        return courierNumberDTO;
    }

    @Override
    public OrderDTO orderdetail(String orderId) {
        //判断订单是否存在
        if (!orderRepository.existsById(orderId)){
            throw new ChanelException(ResultEnum.ORDER_NOT_EXIST);
        }
        //查询订单详细信息
        OrderDTO orderDTO = new OrderDTO();
        OrderEntity orderEntity = orderRepository.findByOrderId(orderId);
        //返回订单详情
        BeanUtils.copyProperties(orderEntity, orderDTO);
        return orderDTO;
    }

    @Override
    public Page<OrderEntity> orderList(Pageable pageable) {
        return orderRepository.findAll(pageable);
    }

    @Override
    public Page<OrderEntity> findOrder(String buyerPhone, Pageable pageable) {
        return orderRepository.findByBuyerPhoneContainingOrderByCreateTimeDesc(buyerPhone, pageable);
    }

    @Override
    public Page<OrderEntity> auditOrderList(int orderStatus, Pageable pageable) {
        return orderRepository.findAllByOrderStatusOrderByCreateTimeDesc(orderStatus, pageable);
    }

    @Override
    public Page<OrderEntity> deliveryOrderList(int deliveryStatus, int orderStatus, Pageable pageable) {
        return orderRepository.findAllByDeliveryStatusAndOrderStatusOrderByCreateTimeDesc(deliveryStatus, orderStatus, pageable);
    }
}
